package com.ysd.lis.service.cri.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.cri.CriCommDto;
import com.ysd.lis.entity.cri.CriTemplateDesc;
import com.ysd.lis.mapper.cri.CriTemplateDescMapper;
import com.ysd.lis.request.cri.CriCommParam;
import com.ysd.lis.service.cri.CriTemplateDescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-02-20
 */
@Service
public class CriTemplateDescServiceImpl extends ServiceImpl<CriTemplateDescMapper, CriTemplateDesc> implements CriTemplateDescService {
    @Autowired
    CriTemplateDescMapper criTemplateDescMapper;

    @Override
    public Result findCriTemplateList(CriCommParam param) {
        MPJLambdaWrapper<CriTemplateDesc> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(CriTemplateDesc::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(CriTemplateDesc::getDescCode, param.getSeachValue())
                    .or().like(CriTemplateDesc::getDescName, param.getSeachValue())
                    .or().like(CriTemplateDesc::getDescText,param.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(CriTemplateDesc::getCreateTime);
        List<CriTemplateDesc> supplierPage = criTemplateDescMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result findCriTemplatePageList(CriCommParam param) {
        Page<CriTemplateDesc> page = new Page<>();
        page.setCurrent(param.getPageIndex());
        page.setSize(param.getPageSize());
        MPJLambdaWrapper<CriTemplateDesc> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(CriTemplateDesc::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(CriTemplateDesc::getDescCode, param.getSeachValue())
                    .or().like(CriTemplateDesc::getDescName, param.getSeachValue())
                    .or().like(CriTemplateDesc::getDescText,param.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(CriTemplateDesc::getCreateTime);
        Page<CriTemplateDesc> supplierPage = criTemplateDescMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addCriTemplate(CriTemplateDesc criTemplateDesc) {
        int insert = criTemplateDescMapper.insert(criTemplateDesc);
        if(insert>0){
            return Result.succ(1,"添加成功",criTemplateDesc);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editCriTemplate(CriTemplateDesc supplier) {
        int updateById = criTemplateDescMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteCriTemplate(CriCommDto dto) {
        List<CriTemplateDesc> supplierList = dto.getCriTemplateList();
        List<String> ids = supplierList.stream().map(CriTemplateDesc::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
